<template>
  <div class="login-bg">
    <div class="login-card">
      <h2>测盟汇管理系统</h2>
      <el-form :model="form" :rules="rules" ref="formRef" class="login-form" @keyup.enter="submitLogin">
        <el-form-item prop="userName">
          <el-input v-model="form.userName" placeholder="用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" show-password />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="form.rememberMe">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;" :loading="loading" @click="submitLogin">登录</el-button>
        </el-form-item>
      </el-form>
      <el-button type="text" style="width: 100%; margin-top: 8px;" @click="goRegister">没有租户账号？去注册</el-button>
      <el-dialog title="登录失败" v-model="errorDialogVisible" width="350px" show-close>
        <span>{{ errorMessage }}</span>
        <template #footer>
          <el-button type="primary" @click="errorDialogVisible = false">关闭</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import UserService from '../services/UserService';
import User from './User.vue';

const router = useRouter();
const form = reactive({
  userName: '',
  password: '',
  rememberMe: false
});
const rules = {
  userName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};
const formRef = ref();
const loading = ref(false);
const errorDialogVisible = ref(false);
const errorMessage = ref('');

onMounted(() => {
  // 自动填充记住的用户名
  const remembered = localStorage.getItem('rememberUserName');
  if (remembered) {
    form.userName = remembered;
    form.rememberMe = true;
  }
});

function showError(msg) {
  errorMessage.value = msg;
  errorDialogVisible.value = true;
}

function submitLogin() {

  formRef.value.validate(async (valid) => {
    if (!valid) return;
    loading.value = true;
    try {
      const res = await UserService.login(form.userName, form.password);
      console.log(res + 84);
      if (res.code === 200 && res.data && res.data.token) {
        localStorage.setItem('token', res.data.token);
        if (form.rememberMe) {
          localStorage.setItem('rememberUserName', form.userName);
        } else {
          localStorage.removeItem('rememberUserName');
        }
        localStorage.setItem('currentUser', JSON.stringify(res.data));
        UserService.loadCurrentUser();
        router.push('/news');

      } else {
        console.log(e + 654);

        showError(res.message || '登录失败');
      }
    } catch (e) {
      console.log(e);
      showError(e || '登录失败');
    } finally {
      loading.value = false;
    }
  });
}

function goRegister() {
  router.push('/register');
}
</script>

<style scoped>
.login-bg {
  width: 100vw;
  height: 100vh;
  background: url('/src/assets/background.png') center center/cover no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.15);
  padding: 36px 32px 24px 32px;
  min-width: 340px;
  min-height: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login-card h2 {
  margin-bottom: 24px;
  font-weight: bold;
  color: #333;
  letter-spacing: 2px;
}

.login-form {
  width: 100%;
}
</style>